/**
 * 封装axios请求
 */

import axios from 'axios'


//非组件模块使用elementUI
import { Message } from 'element-ui'

import { baseURL } from './baseURL'

//创建axios实例
const request = axios.create({ 
    baseURL:baseURL
})

//请求拦截器
request.interceptors.request.use(
    function (config) { 
        let result = JSON.parse(window.localStorage.getItem("result"));

        if(result) { 
            config.headers.Authorization ="Bearer " + result.token
        }


        return config
    },
    function (error) { 
        return Promise.reject(error)
    }
)

//响应拦截器
request.interceptors.response.use(function (response) {
        return response
    },function (error) { 

        const status = error.response.status
        console.log("响应拦截status",status)

        if (status === 401) {
            Message.warning("登录状态过期，请重新登录")
            window.localStorage.removeItem("result")
            this.$router.push("/login")
        } else if (status >= 500) {
            Message.error("服务器内部异常")
        }

        return Promise.reject(error)
    }

)




export default request